package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.constant.ArticleBehaviorRedisPrefixConstants;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.pojo.ApCollection;
import com.heima.article.service.ApCollectionService;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.common.util.JsonUtils;
import com.heima.model.article.dtos.ArticleBehaviorShowDTO;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.spring.web.json.Json;

/**
 * <p>
 * APP收藏信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
@Slf4j
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements ApCollectionService {


    @Autowired
    StringRedisTemplate redisTemplate;


    /**
     * 收藏行为
     *
     * @param dto
     */
    @Override
    public void collectionBehavior(CollectionBehaviorDto dto) {
        if (dto == null) {
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //获取当前id
        Integer userId = UserThreadLocalUtils.getUserId();

        String collectionKey = ArticleBehaviorRedisPrefixConstants.BEHAVIOR_COLLECTION_PREFIX + ":" + userId;
        String showKey = ArticleBehaviorRedisPrefixConstants.BEHAVIOR_SHOW_PREFIX + ":" + userId;

        //String isCollection = (String) redisTemplate.opsForHash().get(collectionKey, dto.getArticleId());
        if (dto.getArticleId() != null) {
            String isShow = (String) redisTemplate.opsForHash().get(showKey, dto.getArticleId());

            //1.1判断用户是否注册
            if (userId == null) {
                //未注册
                log.info("未注册，请先注册");
                throw new LeadException(AppHttpCodeEnum.AD_USER_NOT_EXIST);
            }
            //1.2判断用户是否登录
            if (userId <= 0) {
                //没有登录
                log.info("未登录，请先登录");
                //无权限登录
                throw new LeadException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
            }


            //2.判断是否收藏
            if (dto.getOperation() == 1) {     //1 收藏

                //2.1.1用户收藏行为
                redisTemplate.opsForHash().put(collectionKey, dto.getArticleId() + "", JSON.toJSONString(dto));

                //2.1.2用户行为
                //判断用户行为是否为空
                if (StringUtils.isEmpty(isShow)) {
                    ArticleBehaviorShowDTO articleBehaviorShowDTO = new ArticleBehaviorShowDTO();
                    articleBehaviorShowDTO.setIscollection(true);
                    redisTemplate.opsForHash().put(showKey, dto.getArticleId() + "", JSON.toJSONString(articleBehaviorShowDTO));
                } else {
                    ArticleBehaviorShowDTO showDTO = JsonUtils.toBean(isShow, ArticleBehaviorShowDTO.class);
                    if (showDTO != null) {
                        showDTO.setIscollection(true);
                        redisTemplate.opsForHash().put(showKey, dto.getArticleId() + "", JSON.toJSONString(showDTO));
                    } else {
                        ArticleBehaviorShowDTO articleBehaviorShowDTO = new ArticleBehaviorShowDTO();
                        articleBehaviorShowDTO.setIscollection(true);
                        redisTemplate.opsForHash().put(showKey, dto.getArticleId() + "", JSON.toJSONString(articleBehaviorShowDTO));
                    }

                }
            } else if (dto.getOperation() == 0) {      //0 取消收藏
                //2.1.1收藏行为
                redisTemplate.opsForHash().put(collectionKey, dto.getArticleId() + "", JSON.toJSONString(dto));
                //2.1.2用户行为
                if (StringUtils.isEmpty(isShow)) {
                    ArticleBehaviorShowDTO articleBehaviorShowDTO = new ArticleBehaviorShowDTO();
                    articleBehaviorShowDTO.setIscollection(false);
                    redisTemplate.opsForHash().put(showKey, dto.getArticleId() + "", JSON.toJSONString(articleBehaviorShowDTO));
                } else {
                    ArticleBehaviorShowDTO showDTO = JsonUtils.toBean(isShow, ArticleBehaviorShowDTO.class);
                    if (showDTO != null) {
                        showDTO.setIscollection(false);
                        redisTemplate.opsForHash().put(showKey, dto.getArticleId() + "", JSON.toJSONString(showDTO));
                    } else {
                        ArticleBehaviorShowDTO articleBehaviorShowDTO = new ArticleBehaviorShowDTO();
                        articleBehaviorShowDTO.setIscollection(false);
                        redisTemplate.opsForHash().put(showKey, dto.getArticleId() + "", JSON.toJSONString(articleBehaviorShowDTO));
                    }

                }
            }

        }
    }
}